/**
 * @file index.js
 * @author swan
 */
const app = getApp()

Page({
    data: {
        userInfo: {},
        info: {},
        banner: [],
        contach: {},
        news: [],
        currentNum: 0,
        product: [],
        hasUserInfo: false,
        canIUse: swan.canIUse('button.open-type.getUserInfo')
    },
    onShow() {
        app.globalSeo().then(res => {
            // console.log(res);
            swan.setPageInfo({

                title: res.seo_title,
                keywords: res.seo_keywords,
                description: res.seo_description,
                articleTitle: res.seo_articleTitle,
                image: res.seo_img,
                // releaseDate: '2019-01-02 12:01:30',
            })
        })


    },
    onLoad() {
        var that = this;
        var sysinfo = swan.getStorageSync('info');
        app.head_backgroup(sysinfo);
        if (sysinfo == '') {
            app.globalSetting().then(res => {
                for (let index in res.nav) {
                    var item = res.nav[index];
                    if (item.url == "/pages/index/index" && app.globalData.currentNum == -1) {
                        app.globalData.currentNum = index;
                        break;
                    }
                }
                // swan.setNavigationBarTitle({
                //     title: res.sitename
                // })


                this.setData({
                    'info': res,
                    'currentNum': app.globalData.currentNum
                });
            });
        } else {
            for (let index in sysinfo.nav) {
                var item = sysinfo.nav[index];
                if (item.url == "/pages/index/index" && app.globalData.currentNum == -1) {
                    app.globalData.currentNum = index;
                    break;
                }
            }
            // swan.setNavigationBarTitle({
            //     title: sysinfo.sitename
            // })

            this.setData({
                'info': sysinfo,
                'currentNum': app.globalData.currentNum
            });

        }

        // console.log(app.globalData.currentNum);

        swan.request({
            url: app.util.url('entry/baiduapp/Index'),
            method: 'GET',
            dataType: 'json',
            data: {
                uniacid: app.siteInfo.uniacid,
                m: app.siteInfo.mName,
            },
            header: {
                'content-type': 'application/json' // 默认值
            },
            success: function (res) {
                var indeXinfo = res.data.data;
                that.setData({
                    'banner': indeXinfo.banner,
                    'contact': indeXinfo.contact,
                    'news': indeXinfo.news,
                    'product': indeXinfo.product
                });
            },
            fail: function (err) {

                console.log('错误码：' + err.errCode);
                console.log('错误信息：' + err.errMsg);
            }
        });



    },
    getUserInfo(e) {
        swan.login({
            success: () => {
                swan.getUserInfo({
                    success: (res) => {
                        this.setData({
                            userInfo: res.userInfo,
                            hasUserInfo: true
                        });
                    },
                    fail: () => {
                        this.setData({
                            userInfo: e.detail.userInfo,
                            hasUserInfo: true
                        });
                    }
                });
            },
            fail: () => {
                swan.showModal({
                    title: '未登录',
                    showCancel: false
                });
            }
        });
    },
    tel(e) {
        app.tel(e.currentTarget.dataset.val);
    },
    http_tab(e) {

        app.tabNav(e);
    },
    http_navigator(e) {
        app.tonav(e);
    },
    bannerAction(event) {
        var id = event.currentTarget.dataset.id;
        var cs = this.data.banner[id];

        // console.log(cs);
        if (cs.type_skip == 1) {
            app.bannerUrl(cs.url, cs.id);
        }
        if (cs.type_skip == 2) {
            app.globalData.currentNum = -1;
            app.bannerUrl(cs.url, cs.id);
        }
    },
})
